//
//  NewsViewController.m
//  NEWS_W_Y_S
//
//  Created by lanou3g on 15/10/14.
//  Copyright © 2015年 lanou3g.jake. All rights reserved.
//

#import "NewsViewController.h"
#import "AFNetworking.h"
#import "NewsDetailViewController.h"

#import "MBProgressHUD.h"
#import "NewsDataBaseHandle.h"
#import "NewsModel.h"
#import "HeadView.h"
#import "OneImageTableViewCell.h"
#import "ThreeImageTableViewCell.h"
#import "OneLongImageTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "HeadViewOneImage.h"
#import "MJRefresh.h"
@interface NewsViewController ()<UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate>
{
    float _startPointX;
    float _xx;
    UIView *_navView;
    UIView *_topNaviV;
    UIView *_selectTabV;
    UIScrollView *_scrollV;
    
}


//@property(nonatomic,strong)UITableView * tableView;
@property (strong, nonatomic) NSMutableArray *scrollTableViews;
// 菊花视图
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) MBProgressHUD *hud;
@property (strong, nonatomic) HeadView *headerView;
@property (nonatomic, strong) HeadViewOneImage *headerViewTwo;
@property (nonatomic, retain) NewsDataBaseHandle *sharedInstance;
@property(nonatomic,weak)UIAlertController * alertView;



@end

//static int i = 30;
NSInteger index1 = 0;
NSInteger index2 = 0;
NSInteger index3 = 0;

@implementation NewsViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    // 初始化
    self.titleArray2 = [NSMutableArray array];
    _scrollTableViews = [[NSMutableArray alloc] init];
    self.titleArray = [NSMutableArray array];
    self.allUrlArray = [NSMutableArray array];
    self.sharedInstance = [NewsDataBaseHandle sharedInstance];
    
    [self loadProgressHud];
    // 打开数据库
    [self.sharedInstance openDB];
    [self.sharedInstance openDB1];
    [self analysis];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}


#pragma mark 解析第一层数据
-(void)analysis{
    
    AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:YES];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager setSecurityPolicy:securityPolicy];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:kURLM parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject != nil) {
            [_hud hide:YES];
            
            NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            
            NSArray *array = [NSArray arrayWithArray:dict[@"tList"]];
            _allUrlArray = [NSMutableArray array];
            _titleArray = [NSMutableArray array];
            //把这几个模块剔除在外
            for (NSDictionary *dict in array) {
                if ([dict[@"tname"] isEqualToString:@"头条"] || [dict[@"tname"] isEqualToString:@"画报"] || [dict[@"tname"] isEqualToString:@"原创"] || [dict[@"tname"] isEqualToString:@"轻松一刻"]) {
                    continue;
                }
                [self.titleArray addObject:dict[@"tname"]];
                [self.allUrlArray addObject:dict[@"tid"]];
                if ([dict[@"tname"] isEqualToString:@"值得买"]) {
                    break;
                }
                
            }
            //第二次解析数据:解析出详细的信息
            [self getData:0 string:kURL];
            [self setContentScrollView];
            [self setTitleScrollView];
            
            [self initDownTables];
            
        }
        //[self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    
    
}




#pragma mark - 获得数据
- (void)getData:(NSInteger)pageNumber string:(NSString*)string{
    
    // 当页码数为0时，设置index为0，以避免出现负数的情况
    //减一,即为当前页的前一页
    int index = (int)(pageNumber - 1);
    if (pageNumber == 0) {
        index = 0;
    }
    
    // 循环下载数据，分别下载当前页，当前页的前一页，以及当前页的后一页的数据
    for (int i = index; i <= index + 2; i++) {
        
        
        AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
        [securityPolicy setAllowInvalidCertificates:YES];
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager setSecurityPolicy:securityPolicy];
        
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        NSString * str = self.allUrlArray[i];
        
        
        NSString * urlString = [NSString stringWithFormat:string,str];
        
        [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            if (responseObject != nil) {
                
                // 判断栏目是否存在，如果存在的话就更新数据，不存在的话就插入数据
                if ([self.sharedInstance searchNewsTitle:self.titleArray[i]]) {
                    [self.sharedInstance upDatesearchAllNews:self.titleArray[i] newsData:responseObject];
                }else{
                    [self.sharedInstance insertNewsInfo:self.titleArray[i] newsData:responseObject];
                }
                //存放所有新闻的信息
                self.headLineInfoDiat = [NSMutableDictionary dictionary];
                self.headLineInfoDiat = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
                //字典中放的key是 : 数据接口
                //value 对应的是最外层的大字典
                NSArray *keyArray = [self.headLineInfoDiat allKeys];
                
                NSString *string = [keyArray objectAtIndex:0];//相当于firstObject
                
                
                
                NSArray *array = [NSArray arrayWithArray:self.headLineInfoDiat[string]];
                
                [self selectData:i arrayInfo:array];
                
            }
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
        }];
        
        
    }
    
}


#pragma mark - 选择tableView的数据
- (void)selectData:(int)index arrayInfo:(NSArray*)array
{
    // 由于有三个重用的tableView，所以利用页码对3取余的余数来判断是三个中的哪个tableView
    if (index % 3 == 0) {
        // 把字典封装成对象
        self.allModelArray = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            NewsModel *newModel = [[NewsModel alloc] init];
            [newModel setValuesForKeysWithDictionary:dict];
            if (!([newModel.source isEqualToString:@"网易新媒体"] || [newModel.skipType isEqualToString:@"live"] || [newModel.source isEqualToString:@"江南晚报"] || [newModel.skipType isEqualToString:@"video"])) {
                [self.allModelArray addObject:newModel];
            }
            
        }
        self.newm = self.allModelArray[0];
        [self.allModelArray removeObjectAtIndex:0];
        [(UITableView*)[self.view viewWithTag:3000] reloadData];
        
        // 将获得的数据里的第一组数据取出，然后将其从全部的数据组里移除，以方便将标题信息与普通信息分离
        
    }else if(index % 3 == 1){
        self.allModelArray1 = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            NewsModel *newModel = [[NewsModel alloc] init];
            [newModel setValuesForKeysWithDictionary:dict];
            if (!([newModel.source isEqualToString:@"网易新媒体"] || [newModel.skipType isEqualToString:@"live"] || [newModel.source isEqualToString:@"江南晚报"] || [newModel.skipType isEqualToString:@"video"])) {
                [self.allModelArray1 addObject:newModel];
            }
        }
        self.newm1 = self.allModelArray1[0];
        [self.allModelArray1 removeObjectAtIndex:0];
        [(UITableView*)[self.view viewWithTag:3001] reloadData];
        
        
        
    }else{
        self.allModelArray2 = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            NewsModel *newModel = [[NewsModel alloc] init];
            [newModel setValuesForKeysWithDictionary:dict];
            if (!([newModel.source isEqualToString:@"网易新媒体"] || [newModel.skipType isEqualToString:@"live"] || [newModel.source isEqualToString:@"江南晚报"] || [newModel.skipType isEqualToString:@"video"])) {
                [self.allModelArray2 addObject:newModel];
            }
        }
        self.newm2 = self.allModelArray2[0];
        [self.allModelArray2 removeObjectAtIndex:0];
        [(UITableView*)[self.view viewWithTag:3002] reloadData];
        
        
        
    }
    
    
}

#pragma mark --初始化下方的TableViews
-(void)initDownTables{
    
    for (int i = 0; i < 3; i ++) {
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(i * self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height - 94) style:UITableViewStylePlain];
        tableView.frame = CGRectMake(i * self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height - 94);
        tableView.tag = i + 3000;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.showsVerticalScrollIndicator = NO;
        
        // 添加下拉刷新和上拉加载
        //        tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(setupRefresh)];
        //        [tableView.header beginRefreshing];
        //
        //        tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(beginRefreshing)];
        
        [_scrollTableViews addObject:tableView];
        [self.contentScrollView addSubview:tableView];
        
        // 避免cell的分割线向右偏移
        if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
            
            [tableView setSeparatorInset:UIEdgeInsetsZero];
            
        }
        
        if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            
            [tableView setLayoutMargins:UIEdgeInsetsZero];
            
        }
        
        
    }
    
    
    
}


#pragma mark - 设置第一种headerView
- (HeadView*)setHeader:(NewsModel*)newsModel
{
    if (newsModel == nil) {
        NSLog(@"===%@", newsModel);
        
        
        return nil;
    }
    _headerView = [[HeadView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 220)];
    
    [_headerView.iamge1 sd_setImageWithURL:[NSURL URLWithString:newsModel.imgsrc] placeholderImage:[UIImage imageNamed:kPlacePhoto]];
    [_headerView.scrollView addSubview:[self buttonImage:_headerView.iamge1]];
    
    [_headerView.iamge2 sd_setImageWithURL:[NSURL URLWithString:[newsModel.imgextra[0] objectForKey:@"imgsrc"]] placeholderImage:[UIImage imageNamed:kPlacePhoto]];
    [_headerView.scrollView addSubview:[self buttonImage:_headerView.iamge2]];
    
    [_headerView.iamge3 sd_setImageWithURL:[NSURL URLWithString:[newsModel.imgextra[1] objectForKey:@"imgsrc"]] placeholderImage:[UIImage imageNamed:kPlacePhoto]];
    [_headerView.scrollView addSubview:[self buttonImage:_headerView.iamge3]];
    
    [_headerView.image4 sd_setImageWithURL:[NSURL URLWithString:[newsModel.imgextra[1] objectForKey:@"imgsrc"]] placeholderImage:[UIImage imageNamed:kPlacePhoto]];
    [_headerView.scrollView addSubview:[self buttonImage:_headerView.image4]];
    
    [_headerView.image5 sd_setImageWithURL:[NSURL URLWithString:newsModel.imgsrc] placeholderImage:[UIImage imageNamed:kPlacePhoto]];
    [_headerView.scrollView addSubview:[self buttonImage:_headerView.image5]];
    _headerView.titleLabel.text = newsModel.title;
    _headerView.scrollView.userInteractionEnabled = YES;
    return _headerView;
}

#pragma mark - 设置HeaderView上的button
- (UIButton*)buttonImage:(UIImageView*)image1
{
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = image1.frame;
    [button setBackgroundImage:image1.image forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(push:) forControlEvents:(UIControlEventTouchUpInside)];
    return button;
}

-(void)push:(UIButton*)sender{
    NSLog(@"HeaderView上的button");
}


#pragma mark - 设置第二种headerView
- (HeadViewOneImage*)setHeaderTwo:(NewsModel*)newsModel
{
    self.headerViewTwo = [[HeadViewOneImage alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 220)];
    [self.headerViewTwo.iamge sd_setImageWithURL:[NSURL URLWithString:newsModel.imgsrc] placeholderImage:[UIImage imageNamed:kPlacePhoto]];
    [self.headerViewTwo addSubview:[self buttonImage:self.headerViewTwo.iamge]];
    self.headerViewTwo.titleLabel.text = newsModel.title;
    return self.headerViewTwo;
}



#pragma mark - 根据tableView的tag值判断所对应的tableView，以获取对应的数据,并添加对应的headerView
- (NewsModel*)selectDataByTagOfTableView:(UITableView*)tableView modelOne:(NewsModel*)newsModel modelTwo:(NewsModel*)newsModel1 indexPath:(NSInteger)index
{
    if (tableView.tag == 3000) {
        newsModel = self.allModelArray[index];
        newsModel1 = self.newm;
        [self setTopImages:tableView model:newsModel1];
        return newsModel;
        
    }else if (tableView.tag == 3001){
        newsModel = self.allModelArray1[index];
        newsModel1 = self.newm1;
        [self setTopImages:tableView model:newsModel1];
        return newsModel;
        
    }else{
        newsModel = self.allModelArray2[index];
        newsModel1 = self.newm2;
        [self setTopImages:tableView model:newsModel1];
        return newsModel;
        
    }
}

#pragma mark - 设置顶部轮播图
- (void)setTopImages:(UITableView*)tableView model:(NewsModel*)newsModel
{
    
    if (newsModel.imgextra.count != 0) {
        
        
        [tableView setTableHeaderView:[self setHeader:newsModel]];
        
    }else{
        
        [tableView setTableHeaderView:[self setHeaderTwo:newsModel]];
    }
}

#pragma mark - 避免cell的分割线偏移
- (void)wipeOutCelloffset:(UITableViewCell*)cell
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
}


#pragma mark - 根据数据中不同的key值判断该用哪种cell
- (UITableViewCell*)selectCellBykeyOfModel:(NewsModel*)newsModel tableView:(UITableView*)tableView
{
    if (newsModel.imgextra) {
        static NSString *identifier = @"reuse";
        ThreeImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[ThreeImageTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            
            
        }
        [self wipeOutCelloffset:cell];
        cell.newsModel = newsModel;
        return cell;
    }else if(newsModel.editor){
        static NSString *identifier = @"reuseIdent";
        OneLongImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[OneLongImageTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            
            
        }
        [self wipeOutCelloffset:cell];
        cell.newsModel = newsModel;
        return cell;
    }else{
        
        static NSString *identifier = @"reuseIdentifier";
        OneImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[OneImageTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            
            
        }
        [self wipeOutCelloffset:cell];
        cell.newsModel = newsModel;
        return cell;
    }
}

#pragma mark - scrollView的代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = (NSInteger)(self.contentScrollView.contentOffset.x / self.view.bounds.size.width);
    NSLog(@"index:%ld",index);
    int count = 0;
    if ([self.titleArray[index] isEqualToString:@"头条"]) {
        count = 1;
    }
    for (NSString *str in self.titleArray2) {
        if ([str isEqualToString:self.titleArray[index]]) {
            count++;
        }
    }
    [self.titleArray2 addObject:self.titleArray[index]];
    // 还原button的字体
    for (int i = 0; i < self.titleArray.count; i++) {
        UIButton *btn = (UIButton*)self.titleScrollView.subviews[i];
        btn.titleLabel.font = [UIFont systemFontOfSize:17];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    for (int i = 0; i < self.titleArray.count; i++) {
        [((UIButton*)_selectTabV.subviews[i]) setBackgroundColor:[UIColor whiteColor]];
    }
    
    NSString *str = @"移动互联";
    NSString *str2 = @"云课堂";
    if ([scrollView isEqual:self.contentScrollView]) {
        // 改变titleView，即button下面滑动的红色横杠的位置
        if ([self.titleArray[index] length] == [str length]) {
            self.titleView.frame = CGRectMake(self.contentScrollView.contentOffset.x / self.view.bounds.size.width * 90, 27, 90, 3);
        }else if([self.titleArray[index] length] == [str2 length]){
            self.titleView.frame = CGRectMake(self.contentScrollView.contentOffset.x / self.view.bounds.size.width * 90 + 10, 27, 70, 3);
        }else{
            self.titleView.frame = CGRectMake(self.contentScrollView.contentOffset.x / self.view.bounds.size.width * 90 + 25, 27, 40, 3);
        }
        
        
    }
    
    // 约束分类标题栏的ScrollView的滑动范围
    
    // 改变button的字体
    
    UIButton * btn = (UIButton*)[self.view viewWithTag:index + 1000];
    //    btn.titleLabel.font = [UIFont systemFontOfSize:22];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    if ([scrollView isEqual:self.contentScrollView] && index2 != index) {
        [self getData:index string:kURL];
        
    }
    
    // 添加tableView
    [self updateTableWithPageNumber:index + 1];
    [self updateTableWithPageNumber:index - 1];
    [self updateTableWithPageNumber:index];
    
    [((UIButton*)_selectTabV.subviews[index]) setBackgroundColor:[UIColor redColor]];
    
    // 设置换页才刷新
    //    if ([scrollView isEqual:self.contentScrollView] && index2 != index && count == 0) {
    //        [self getData:index string:kURL];
    //        UITableView *tableView = (UITableView*)[self.view viewWithTag:(index % 3) + 3000];
    
    //        tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(setupRefresh)];
    //        [tableView.header beginRefreshing];
    //
    //        tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(beginRefreshing)];
    
    [self.hud setHidden:YES];
    // 移除无网络状态下的占位图
    //        [(UIImageView*)[self.view viewWithTag:7000] removeFromSuperview];
    //}
    index2 = index;
    
}

// 开始拖拽的时候记录下contentScrollView的偏移量
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:self.contentScrollView]) {
        _startPointX = self.contentScrollView.contentOffset.x;
        
    }
    self.titleView.frame = CGRectMake(self.contentScrollView.contentOffset.x / self.view.bounds.size.width * 90 + 25, 27, 40, 3);
    
    
    
}


// 正在滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if ([scrollView isEqual:self.contentScrollView]) {
        
        [self changeView:self.contentScrollView.contentOffset.x];
        self.titleScrollView.contentOffset = CGPointMake(self.contentScrollView.bounds.origin.x / self.view.bounds.size.width * 90 - 90, 0);
        CGFloat offsetMax = self.titleScrollView.contentSize.width - self.titleScrollView.bounds.size.width;
        if (self.titleScrollView.contentOffset.x < 0) {
            self.titleScrollView.contentOffset = CGPointMake(0, 0);
        }else if(self.titleScrollView.contentOffset.x > offsetMax){
            self.titleScrollView.contentOffset = CGPointMake(offsetMax, 0);
        }
    }
    
}


#pragma mark --根据scrollView的滚动位置复用tableView
-(void) updateTableWithPageNumber: (NSUInteger) pageNumber{
    int tabviewTag = pageNumber % 3;
    
    CGRect tableNewFrame = CGRectMake(pageNumber * self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height - 94);
    
    UITableView *reuseTableView = _scrollTableViews[tabviewTag];
    reuseTableView.frame = tableNewFrame;
    [reuseTableView reloadData];
}


#pragma mark - 设置分类标题ScrollView
- (void)setTitleScrollView
{
    self.titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width - 30, 30)];
    
    _titleScrollView.showsVerticalScrollIndicator = NO;
    _titleScrollView.showsHorizontalScrollIndicator = NO;
    _titleScrollView.bounces = NO;
    _titleScrollView.backgroundColor = [UIColor whiteColor];
    _titleScrollView.contentSize = CGSizeMake(90 * self.titleArray.count , 30);
    self.titleScrollView.delegate = self;
    [self.view addSubview:_titleScrollView];
    // 添加button
    for (int i = 0; i < [self.titleArray count]; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake((50 + 40) * i, 0, 90, 30);
        button.titleLabel.textAlignment = NSTextAlignmentLeft;
        [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [button setTintColor:[UIColor blackColor]];
        button.titleLabel.font = [UIFont systemFontOfSize:17];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 1000 + i;
        if (i == 0) {
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
        [self.titleScrollView addSubview:button];
        
    }
    
    // 设置button下面的红色横杠
    self.titleView = [[UIView alloc]initWithFrame:CGRectMake(25, 27, 40, 3)];
    _titleView.backgroundColor = [UIColor redColor];
    [self.titleScrollView addSubview:_titleView];
}

#pragma mark - 分类标题栏上button的点击事件
- (void)titleClick:(UIButton*)button
{
    
    for (int i = 0; i < self.titleArray.count; i++) {
        if (button.tag == 1000 + i) {
            
            [self.contentScrollView scrollRectToVisible:CGRectMake(self.view.bounds.size.width * i, 0, self.view.bounds.size.width, self.view.bounds.size.height - 94) animated:YES];
        }
    }
    
}


#pragma mark - 设置内容部分的ScrollView
- (void)setContentScrollView
{
    _contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 94, self.view.bounds.size.width, self.view.bounds.size.height  )];
    _contentScrollView.backgroundColor = [UIColor whiteColor];
    _contentScrollView.delegate = self;
    _contentScrollView.contentSize = CGSizeMake(self.view.bounds.size.width * self.titleArray.count , self.view.bounds.size.height - 94);
    _contentScrollView.pagingEnabled = YES;
    _contentScrollView.showsVerticalScrollIndicator = NO;
    _contentScrollView.showsHorizontalScrollIndicator = NO;
    _contentScrollView.bounces = NO;
    _contentScrollView.pagingEnabled = YES;
    
    [self.view addSubview:_contentScrollView];
    
}
/**
 *
 *  @return <#return value description#>
 */


// 改变frame
- (void)changeView:(float)x
{
    NSString *str = @"移动互联";
    NSString *str2 = @"云课堂";
    NSInteger index = (NSInteger)(self.contentScrollView.contentOffset.x / self.view.bounds.size.width);
    float xx = x * (90 / self.view.frame.size.width);
    if ([self.titleArray[index] length] == [str length]) {
        [self.titleView setFrame:CGRectMake(xx, self.titleView.frame.origin.y, self.titleView.frame.size.width, self.titleView.frame.size.height)];
        
    }else if([self.titleArray[index] length] == [str2 length]){
        [self.titleView setFrame:CGRectMake(xx + 10, self.titleView.frame.origin.y, self.titleView.frame.size.width, self.titleView.frame.size.height)];
        
    }else{
        [self.titleView setFrame:CGRectMake(xx + 25, self.titleView.frame.origin.y, self.titleView.frame.size.width, self.titleView.frame.size.height)];
        
    }
    
}


#pragma mark - tableView的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    // 根据tableView的tag值判断所对应的tableView，以获取对应的数据
    if (tableView.tag == 3000) {
        for (int i = 0; i < self.allModelArray.count; i++) {
            NewsModel *news = self.allModelArray[i];
            if ([news.source isEqualToString:@"网易新媒体"] || [news.skipType isEqualToString:@"live"]) {
                [self.allModelArray removeObject:news];
            }
        }
        return self.allModelArray.count - 1;
    }else if(tableView.tag == 3001){
        for (int i = 0; i < self.allModelArray1.count; i++) {
            NewsModel *news = self.allModelArray1[i];
            if ([news.source isEqualToString:@"网易新媒体"] || [news.skipType isEqualToString:@"live"]) {
                [self.allModelArray1 removeObject:news];
            }
        }
        return self.allModelArray1.count - 1;
    }else{
        for (int i = 0; i < self.allModelArray2.count; i++) {
            NewsModel *news = self.allModelArray2[i];
            if ([news.source isEqualToString:@"网易新媒体"] || [news.skipType isEqualToString:@"live"]) {
                [self.allModelArray2 removeObject:news];
            }
        }
        return self.allModelArray2.count - 1;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 根据tableView的tag值判断所对应的tableView，以获取对应的数据
    NewsModel *newsModel = [[NewsModel alloc] init];
    if (tableView.tag == 3000) {
        newsModel = self.allModelArray[indexPath.row];
        
    }else if (tableView.tag == 3001){
        newsModel = self.allModelArray1[indexPath.row];
    }else{
        newsModel = self.allModelArray2[indexPath.row];
    }
    
    // 根据不同的数据返回不同的cell高度
    if (newsModel.editor) {
        return 190;
    }else if(newsModel.imgextra){
        return 130;
    }else{
        return 80;
    }
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    tableView.userInteractionEnabled = YES;
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    NewsModel *newsModel = [[NewsModel alloc] init];
    NewsModel *newsModel1 = [[NewsModel alloc] init];
    
    // 根据tableView的tag值判断所对应的tableView，以获取对应的数据,并添加对应的headerView
    newsModel = [self selectDataByTagOfTableView:tableView modelOne:newsModel modelTwo:newsModel1 indexPath:indexPath.row];
    // 根据数据中不同的key值判断该用哪种cell
    return [self selectCellBykeyOfModel:newsModel tableView:tableView];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    NewsDetailViewController *newDetailVC = [[NewsDetailViewController alloc] init];
    
    //[self.navigationController pushViewController:newDetailVC animated:YES];

}



#pragma mark - 延迟加载视图
- (void)loadProgressHud
{
    self.hud = [[MBProgressHUD alloc] initWithFrame:CGRectMake(100, 100, 160, 150)];
    _hud.center = self.view.center;
    //        _hud.backgroundColor = [UIColor whiteColor];
    _hud.labelText = @"loading……";
    [self.view addSubview:_hud];
    [_hud show:YES];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController setNavigationBarHidden:YES];
    self.tabBarController.tabBar.hidden = NO;
}



@end
